package com.zidian.leader.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

import com.zidian.leader.R;
import com.zidian.leader.api.ApiException;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.activity.MySwipeBackActivity;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.NoDataResult;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 修改密码 Activity
 * Created by GongCheng on 2017/2/17.
 */

public class ChangePasswordActivity extends MySwipeBackActivity implements TextWatcher{
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.old_password_et)
    EditText oldPasswordEt;
    @Bind(R.id.old_til)
    TextInputLayout oldTil;
    @Bind(R.id.new_password_et)
    EditText newPasswordEt;
    @Bind(R.id.new_til)
    TextInputLayout newTil;
    @Bind(R.id.confirm_password_et)
    EditText confirmPasswordEt;
    @Bind(R.id.confirm_til)
    TextInputLayout confirmTil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_change_password;
    }

    private void initView() {
        actionBar.setTitle(R.string.change_password);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp);
    }

    @OnClick(R.id.confirm_btn)
    public void onConfirmBtnClick() {
        confirmChange();
    }

    @OnClick(R.id.cancel_btn)
    public void onCancelBtnClick() {
        onBackPressed();
    }

    private void confirmChange() {
        final String oldPassword = oldPasswordEt.getText().toString().trim();
        String newPassword = newPasswordEt.getText().toString().trim();
        String confirmPassword = confirmPasswordEt.getText().toString().trim();
        oldPasswordEt.addTextChangedListener(this);
        newPasswordEt.addTextChangedListener(this);
        confirmPasswordEt.addTextChangedListener(this);
        if (oldPassword.length() == 0) {
            oldTil.setErrorEnabled(true);
            oldTil.setError(getString(R.string.please_input_old_password));
            return;
        }
        if (newPassword.length() == 0) {
            newTil.setErrorEnabled(true);
            newTil.setError(getString(R.string.please_input_new_password));
            return;
        }
        if (!newPassword.equals(confirmPassword)) {
            confirmTil.setErrorEnabled(true);
            confirmTil.setError(getString(R.string.entered_password_diff));
            return;
        }
        //修改密码
        changePassword(oldPassword, newPassword, confirmPassword);
    }

    /**
     * 修改密码
     *
     * @param password 旧密码
     * @param password1 新密码
     * @param password2 确认密码
     */
    private void changePassword(String password, String password1, String password2) {
        subscription = HttpFactory.getInstance().changePassword(HawkUtils.getUsername(),
                password, password1, password2, HawkUtils.getRoleId() + StringConstants.STRING_EMPTY,
                HawkUtils.getToken())
                .map(new Func1<NoDataResult, NoDataResult>() {
                    @Override
                    public NoDataResult call(NoDataResult result) {
                        if (result.getCode() != 200) {
                            throw new ApiException(result.getMessage());
                        } else {
                            return result;
                        }
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<NoDataResult>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Snackbar.make(newPasswordEt, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onNext(NoDataResult result) {
                        Toast.makeText(ChangePasswordActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
                        HawkUtils.clearPreferences();
                        startActivity(LoginActivity.class);
                    }
                });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        oldTil.setErrorEnabled(false);
        newTil.setErrorEnabled(false);
        confirmTil.setErrorEnabled(false);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
